package com.lizhen.createpattern.prototypepattern;

import com.lizhen.createpattern.prototypepattern.impl.Circle;
import com.lizhen.createpattern.prototypepattern.impl.Rectangle;
import com.lizhen.createpattern.prototypepattern.impl.Square;

import java.util.Hashtable;

public class ShapeCache {
    private static Hashtable<String, Shape> shapeMap = new Hashtable();

    public ShapeCache() {
    }

    public static Shape getShape(String shapeId) {
        Shape cachedShape = (Shape) shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(), circle);
        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(), square);
        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(), rectangle);
    }
}
